<template>
  <basic-container>
    <div style="margin-bottom: 20px">
      <el-button type="primary" size="small" @click="handleAdd">添加</el-button>
    </div>
    <el-table
      :data="tableData"
      border
      style="width: 100%"
      height="calc(100vh - 260px)"
      :header-cell-style="{
        backgroundColor: '#fafafa',
        fontWeight: 'bold',
        color: '#252525',
      }"
      :row-style="{ height: '50px' }"
      :cell-style="{ padding: '8px 0' }"
    >
      <el-table-column label="序号" type="index" width="80">
        <template slot-scope="scope">
          <span> {{ (pageNum - 1) * pageSize + (scope.$index + 1) }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="name" label="名称" />
      <el-table-column prop="level" label="等级" />
      <el-table-column prop="alarmColor" label="报警颜色" />
      <el-table-column prop="createBy" label="创建人" />
      <el-table-column prop="remark" label="备注" />
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button type="text" size="small" @click="handleEdit(scope.row)">编辑</el-button>
          <el-button type="text" size="small" @click="handleDelete(scope.row)" style="color: #f56c6c">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <div style="margin-top: 20px; text-align: right">
      <el-pagination
        :total="total"
        :page-size="pageSize"
        background
        layout="total, sizes, prev, pager, next, jumper"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      ></el-pagination>
    </div>

    <el-dialog
      :title="addFormTitle"
      :visible.sync="addFormVisible"
      width="500px"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
      :before-close="handleClose"
    >
      <el-form :model="addForm" :rules="addFormRules" label-width="100px" ref="addFormRef">
        <el-form-item label="名称" prop="name">
          <el-input v-model="addForm.name" style="width: 90%" placeholder="请输入名称" show-word-limit :maxlength="50"/>
        </el-form-item>
        <el-form-item label="报警等级" prop="level">
          <el-input-number 
            v-model="addForm.level"  
            style="width: 90%"
            :min="1"
            :precision="0"
            controls-position="right"
            placeholder="请输入正整数"
          />
        </el-form-item>
        <el-form-item label="报警颜色" prop="alarmColor">
          <el-color-picker v-model="addForm.alarmColor" />
        </el-form-item>
        <el-form-item label="备注">
          <el-input v-model="addForm.remark" style="width: 90%" type="textarea" placeholder="请输入备注" :rows="4" show-word-limit :maxlength="500"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="handleClose">取消</el-button>
        <el-button type="primary" @click="handleConfirm">确定</el-button>
      </div>
    </el-dialog>
  </basic-container>
</template>

<script>
export default {
  name: "alarmPrioritySetting",
  data() {
    return {
      tableData: [
        {
          name: "一级",
          level: "一级",
          alarmColor: "红色",
          createBy: "张三",
          remark: "备注",
        },
      ],
      pageNum: 1,
      pageSize: 10,
      total: 0,

      addFormVisible: false,
      addFormTitle: "",
      addForm: {},
      addFormRules: {
        name: [{ required: true, message: "请输入名称", trigger: "blur" }],
        level: [
          { required: true, message: "请输入等级", trigger: "blur" },
          { validator: this.validatePositiveInteger, trigger: "blur" }
        ],
        alarmColor: [{ required: true, message: "请选择报警颜色", trigger: "change" }],
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    // 验证正整数
    validatePositiveInteger(rule, value, callback) {
      if (value === null || value === undefined || value === "") {
        callback();
        return;
      }
      const num = parseFloat(value);
      if (isNaN(num) || num <= 0 || !Number.isInteger(num)) {
        callback(new Error("请输入正整数"));
      } else {
        callback();
      }
    },
    handleAdd() {
      this.addForm = {};
      this.addFormTitle = "添加";
      this.addFormVisible = true;
    },
    handleEdit(row) {
      this.addForm = row;
      this.addFormTitle = "编辑";
      this.addFormVisible = true;
    },
    handleDelete(row) {
      this.$confirm("确定删除该项吗？", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        // TODO: 调用删除API
        console.log('删除行:', row);
        this.getList();
      });
    },
    handleClose() {
      this.addFormVisible = false;
      this.addForm = {
        name: "",
        level: "",
        alarmColor: "",
        remark: "",
      }
      this.$refs.addFormRef.resetFields();
    },
    handleConfirm() {
      this.$refs.addFormRef.validate((valid) => {
        if (valid) {
          this.addFormVisible = false;
          this.getList();
        }
      });
    },
    handleSizeChange(size) {
      this.pageSize = size;
      this.getList();
    },
    handleCurrentChange(page) {
      this.pageNum = page;
      this.getList();
    },
    getList() {
      this.tableData = [
        {
          name: "一级",
          level: "一级",
          alarmColor: "红色",
          createBy: "张三",
          remark: "备注",
        },
      ];
    },
  },
};
</script>
